package com.qfedu.service.impl;

import com.github.pagehelper.PageHelper;
import com.qfedu.dao.BillReleaseDao;
import com.qfedu.dao.BillDao;
import com.qfedu.dao.DriverDao;
import com.qfedu.entity.Billinfo;
import com.qfedu.entity.Billrelease;
import com.qfedu.entity.Driverinfo;
import com.qfedu.service.BillService;
import com.qfedu.util.Constants;
import com.qfedu.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * projectName: logistics
 * author: CGY
 * time: 2020/10/6 11:16
 * description:
 */
@Service
public class BillServiceImpl implements BillService {
    @Autowired
    private BillDao billDao;
    @Autowired
    private BillReleaseDao billReleaseDao;
    @Autowired
    private DriverDao driverDao;
    @Override
    public List<Billinfo> findNotRelease(Integer pageNum, Integer limit) {
        PageHelper.startPage(pageNum,limit);
        List<Billinfo> list = billDao.findAllByBillState(Constants.BILL_UN_RELEASE);
        return list;
    }

    @Override
    public List<Billinfo> findByPage(Integer pageNum, Integer limit) {
        PageHelper.startPage(pageNum,limit);
        List<Billinfo> list = billDao.findAll();
        return list;
    }
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result addRelease(Billrelease billrelease) {
        //查询票据信息
        Billinfo bill = billDao.findByBillCode(billrelease.getBillCode());
        //将票据分发信息的type设置上
        billrelease.setBillType(bill.getBillType());
        //保存票据分发信息
        Billrelease brelease = billReleaseDao.save(billrelease);
        if(brelease == null){
            return Result.error("添加失败");
        }
        //改变原票据信息的状态
        bill.setBillState(Constants.BILL_UN_SEND);
        billDao.save(bill);
        //查询处司机信息
        Driverinfo driver = driverDao.findById(billrelease.getReceiveBillPerson());
        driver.setState(Constants.DRIVER_STATE_UN_SEND);
        //保存
        driverDao.save(driver);
        return Result.success(brelease);
    }
}
